{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 变量进阶（理解）\n",
    "\n",
    "## 目标\n",
    "\n",
    "* 变量的引用\n",
    "* 可变和不可变类型\n",
    "* 局部变量和全局变量\n",
    "\n",
    "## 01. 变量的引用\n",
    "\n",
    "> * 变量 和 数据 都是保存在 **内存** 中的\n",
    "> * 在 `Python` 中 **函数 的 参数传递** 以及 **返回值** 都是靠 **引用** 传递的\n",
    "\n",
    "### 1.1 引用的概念\n",
    "\n",
    "在 `Python` 中\n",
    "\n",
    "* **变量** 和 **数据** 是分开存储的\n",
    "* **数据** 保存在内存中的一个位置\n",
    "* **变量** 中保存着数据在内存中的地址\n",
    "* **变量** 中 **记录数据的地址**，就叫做 **引用**\n",
    "* 使用 `id()` 函数可以查看变量中保存数据所在的 **内存地址**\n",
    "\n",
    "> 注意：如果变量已经被定义，当给一个变量赋值的时候，本质上是 **修改了数据的引用**\n",
    ">\n",
    "> * 变量 **不再** 对之前的数据引用\n",
    "> * 变量 **改为** 对新赋值的数据引用\n",
    "\n",
    "### 1.2 `变量引用` 的示例\n",
    "\n",
    "在 `Python` 中，变量的名字类似于 **便签纸** 贴在 **数据** 上\n",
    "\n",
    "* 定义一个整数变量 `a`，并且赋值为 `1`\n",
    "\n",
    "| 代码 | 图示 |\n",
    "| :---: | :---: |\n",
    "| a = 1 | ![004_a1tag](./res/004_a1tag.png)|\n",
    "\n",
    "* 将变量 `a` 赋值为 `2`\n",
    "\n",
    "| 代码 | 图示 |\n",
    "| :---: | :---: |\n",
    "| a = 2 | ![005_a2tag](./res/005_a2tag.png)|\n",
    "\n",
    "* 定义一个整数变量 `b`，并且将变量 `a` 的值赋值给 `b`\n",
    "\n",
    "| 代码 | 图示 |\n",
    "| :---: | :---: |\n",
    "| b = a | ![006_ab2tag](./res/006_ab2tag.png)|\n",
    "\n",
    "> 变量 `b` 是第 2 个贴在数字 `2` 上的标签\n",
    "\n",
    "### 1.3 函数的参数和返回值的传递\n",
    "\n",
    "在 `Python` 中，函数的 **实参**/**返回值** 都是是靠 **引用** 来传递来的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "调用函数前 内存地址是 7ffd6d98e470\n",
      "--------------------------------------------------\n",
      "10 在函数内的内存地址是 7ffd6d98e470\n",
      "返回值 100 在内存中的地址是 7ffd6d98efb0\n",
      "--------------------------------------------------\n",
      "调用函数后 实参内存地址是 7ffd6d98e470\n",
      "调用函数后 返回值内存地址是 7ffd6d98efb0\n"
     ]
    }
   ],
   "source": [
    "def test(num):\n",
    "\n",
    "    print(\"-\" * 50)\n",
    "    print(\"%d 在函数内的内存地址是 %x\" % (num, id(num)))\n",
    "\n",
    "    result = 100\n",
    "\n",
    "    print(\"返回值 %d 在内存中的地址是 %x\" % (result, id(result)))\n",
    "    print(\"-\" * 50)\n",
    "\n",
    "    return  result\n",
    "\n",
    "a = 10\n",
    "print(\"调用函数前 内存地址是 %x\" % id(a))\n",
    "\n",
    "r = test(a)\n",
    "\n",
    "print(\"调用函数后 实参内存地址是 %x\" % id(a))\n",
    "print(\"调用函数后 返回值内存地址是 %x\" % id(r))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 02. 可变和不可变类型\n",
    "\n",
    "* **不可变类型**，内存中的数据不允许被修改：\n",
    "    * 数字类型 `int`, `bool`, `float`, `complex`, `long(2.x)`\n",
    "    * 字符串 `str`\n",
    "    * 元组 `tuple`\n",
    "    \n",
    "* **可变类型**，内存中的数据可以被修改：\n",
    "    * 列表 `list`\n",
    "    * 字典 `dict`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "定义列表后的内存地址 1490223415368\n",
      "修改数据后的内存地址 1490223415368\n",
      "定义字典后的内存地址 1490223438296\n",
      "修改数据后的内存地址 1490223438296\n"
     ]
    }
   ],
   "source": [
    "demo_list = [1, 2, 3]\n",
    "\n",
    "print(\"定义列表后的内存地址 %d\" % id(demo_list))\n",
    "\n",
    "demo_list.append(999)\n",
    "demo_list.pop(0)\n",
    "demo_list.remove(2)\n",
    "demo_list[0] = 10\n",
    "\n",
    "print(\"修改数据后的内存地址 %d\" % id(demo_list))\n",
    "\n",
    "demo_dict = {\"name\": \"小明\"}\n",
    "\n",
    "print(\"定义字典后的内存地址 %d\" % id(demo_dict))\n",
    "\n",
    "demo_dict[\"age\"] = 18\n",
    "demo_dict.pop(\"name\")\n",
    "demo_dict[\"name\"] = \"老王\"\n",
    "\n",
    "print(\"修改数据后的内存地址 %d\" % id(demo_dict))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 注意：字典的 `key` **只能使用不可变类型的数据**\n",
    "\n",
    "**注意**\n",
    "\n",
    "1. **可变类型**的数据变化，是通过 **方法** 来实现的\n",
    "2. 如果给一个可变类型的变量，赋值了一个新的数据，**引用会修改**\n",
    "    * 变量 **不再** 对之前的数据引用\n",
    "    * 变量 **改为** 对新赋值的数据引用\n",
    "\n",
    "### 哈希 `(hash)`\n",
    "\n",
    "* `Python` 中内置有一个名字叫做 `hash(o)` 的函数\n",
    "    * 接收一个 **不可变类型** 的数据作为 **参数**\n",
    "    * **返回** 结果是一个 **整数**\n",
    "* `哈希` 是一种 **算法**，其作用就是提取数据的 **特征码（指纹）**\n",
    "    * **相同的内容** 得到 **相同的结果**\n",
    "    * **不同的内容** 得到 **不同的结果**\n",
    "* 在 `Python` 中，设置字典的 **键值对** 时，会首先对 `key` 进行 `hash` 已决定如何在内存中保存字典的数据，以方便 **后续** 对字典的操作：**增、删、改、查**\n",
    "    * 键值对的 `key` 必须是不可变类型数据\n",
    "    * 键值对的 `value` 可以是任意类型的数据\n",
    "\n",
    "## 03. 局部变量和全局变量\n",
    "\n",
    "* **局部变量** 是在 **函数内部** 定义的变量，**只能在函数内部使用**\n",
    "* **全局变量** 是在 **函数外部定义** 的变量（没有定义在某一个函数内），**所有函数** 内部 **都可以使用这个变量**\n",
    "\n",
    "> 提示：在其他的开发语言中，大多 **不推荐使用全局变量** —— 可变范围太大，导致程序不好维护！\n",
    "\n",
    "### 3.1 局部变量\n",
    "\n",
    "* **局部变量** 是在 **函数内部** 定义的变量，**只能在函数内部使用**\n",
    "* 函数执行结束后，**函数内部的局部变量，会被系统回收**\n",
    "* 不同的函数，可以定义相同的名字的局部变量，但是 **彼此之间** 不会产生影响\n",
    "\n",
    "#### 局部变量的作用\n",
    "\n",
    "* 在函数内部使用，**临时** 保存 **函数内部需要使用的数据**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "修改后 20\n",
      "100\n",
      "over\n"
     ]
    }
   ],
   "source": [
    "def demo1():\n",
    "\n",
    "    num = 10\n",
    "\n",
    "    print(num)\n",
    "\n",
    "    num = 20\n",
    "\n",
    "    print(\"修改后 %d\" % num)\n",
    "\n",
    "\n",
    "def demo2():\n",
    "\n",
    "    num = 100\n",
    "\n",
    "    print(num)\n",
    "\n",
    "demo1()\n",
    "demo2()\n",
    "\n",
    "print(\"over\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 局部变量的生命周期\n",
    "\n",
    "* 所谓 **生命周期** 就是变量从 **被创建** 到 **被系统回收** 的过程\n",
    "* **局部变量** 在 **函数执行时** 才会被创建\n",
    "* **函数执行结束后** 局部变量 **被系统回收**\n",
    "* **局部变量在生命周期** 内，可以用来存储 **函数内部临时使用到的数据**\n",
    "\n",
    "### 3.2 全局变量\n",
    "\n",
    "* **全局变量** 是在 **函数外部定义** 的变量，所有函数内部都可以使用这个变量\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "10\n",
      "over\n"
     ]
    }
   ],
   "source": [
    "# 定义一个全局变量\n",
    "num = 10\n",
    "\n",
    "\n",
    "def demo1():\n",
    "\n",
    "    print(num)\n",
    "\n",
    "\n",
    "def demo2():\n",
    "\n",
    "    print(num)\n",
    "\n",
    "demo1()\n",
    "demo2()\n",
    "\n",
    "print(\"over\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**注意**：函数执行时，**需要处理变量时** 会：\n",
    "\n",
    "1. **首先** 查找 **函数内部** 是否存在 **指定名称 的局部变量**，**如果有，直接使用**\n",
    "2. 如果没有，查找 **函数外部** 是否存在 **指定名称 的全局变量**，**如果有，直接使用**\n",
    "3. 如果还没有，程序报错！\n",
    "\n",
    "#### 1) 函数不能直接修改 `全局变量的引用`\n",
    "\n",
    "* **全局变量** 是在 **函数外部定义** 的变量（没有定义在某一个函数内），**所有函数** 内部 **都可以使用这个变量**\n",
    "\n",
    "> 提示：在其他的开发语言中，大多 **不推荐使用全局变量** —— 可变范围太大，导致程序不好维护！\n",
    "\n",
    "* 在函数内部，可以 **通过全局变量的引用获取对应的数据**\n",
    "* 但是，**不允许直接修改全局变量的引用** —— 使用赋值语句修改全局变量的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "demo1--------------------------------------------------\n",
      "100\n",
      "demo2--------------------------------------------------\n",
      "10\n",
      "over\n"
     ]
    }
   ],
   "source": [
    "num = 10\n",
    "\n",
    "\n",
    "def demo1():\n",
    "\n",
    "    print(\"demo1\" + \"-\" * 50)\n",
    "\n",
    "    # 只是定义了一个局部变量，不会修改到全局变量，只是变量名相同而已\n",
    "    num = 100\n",
    "    print(num)\n",
    "\n",
    "\n",
    "def demo2():\n",
    "\n",
    "    print(\"demo2\" + \"-\" * 50)\n",
    "    print(num)\n",
    "\n",
    "demo1()\n",
    "demo2()\n",
    "\n",
    "print(\"over\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "> 注意：只是在函数内部定义了一个局部变量而已，只是变量名相同 —— 在函数内部不能直接修改全局变量的值\n",
    "\n",
    "#### 2) 在函数内部修改全局变量的值\n",
    "\n",
    "* 如果在函数中需要修改全局变量，需要使用 `global` 进行声明"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "demo1--------------------------------------------------\n",
      "100\n",
      "demo2--------------------------------------------------\n",
      "100\n",
      "over\n"
     ]
    }
   ],
   "source": [
    "num = 10\n",
    "\n",
    "\n",
    "def demo1():\n",
    "\n",
    "    print(\"demo1\" + \"-\" * 50)\n",
    "\n",
    "    # global 关键字，告诉 Python 解释器 n 是一个全局变量\n",
    "    global num\n",
    "    # 只是定义了一个局部变量，不会修改到全局变量，只是变量名相同而已\n",
    "    num = 100\n",
    "    print(num)\n",
    "\n",
    "\n",
    "def demo2():\n",
    "\n",
    "    print(\"demo2\" + \"-\" * 50)\n",
    "    print(num)\n",
    "\n",
    "demo1()\n",
    "demo2()\n",
    "\n",
    "print(\"over\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3) 全局变量定义的位置\n",
    "\n",
    "* 为了保证所有的函数都能够正确使用到全局变量，应该 **将全局变量定义在其他函数的上方**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "20\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'c' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-3a8ac42473b1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[0mb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m20\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m \u001b[0mdemo\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     11\u001b[0m \u001b[0mc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m30\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-7-3a8ac42473b1>\u001b[0m in \u001b[0;36mdemo\u001b[1;34m()\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"%d\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"%d\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"%d\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[0mb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m20\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'c' is not defined"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "\n",
    "\n",
    "def demo():\n",
    "    print(\"%d\" % a)\n",
    "    print(\"%d\" % b)\n",
    "    print(\"%d\" % c)\n",
    "\n",
    "b = 20\n",
    "demo()\n",
    "c = 30\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "**注意**\n",
    "\n",
    "* 由于全局变量 c，是在调用函数之后，才定义的，在执行函数时，变量还没有定义，所以程序会报错！\n",
    "\n",
    "**代码结构示意图如下**\n",
    "\n",
    "![001_代码结构示意图-w240](./res/001_代码结构示意图.png)\n",
    "\n",
    "\n",
    "#### 4) 全局变量命名的建议\n",
    "\n",
    "* 为了避免局部变量和全局变量出现混淆，在定义全局变量时，有些公司会有一些开发要求，例如：\n",
    "* 全局变量名前应该增加 `g_` 或者 `gl_` 的前缀\n",
    "\n",
    "> 提示：具体的要求格式，各公司要求可能会有些差异\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
